package br.com.ff.equipamento.entidade;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import br.com.ff.common.StatusEnum;
import br.com.ff.equipamento.to.EquipamentoTO;
import br.com.ff.versao.entidade.Versao;

@Entity
@Table(name="equipamento")
@NamedQueries({
	@NamedQuery(
			name="findDisponiveis",
			query="SELECT e FROM Equipamento e "
				+ "WHERE e.status = true AND e.serial NOT IN ("
				+ "SELECT v.equipamentoSrn FROM Veiculo v)"
			)
})
public class Equipamento {
	 
	@Id
	@Column(name="EQP_ID")
	private Long id;
	
	@Column(name="EQP_SRN")
	private String serial;
	 
	@Column(name="EQP_DDD")
	private Integer ddd;
	 
	@Column(name="EQP_TEL")
	private Integer telefone;
	 
	@Column(name="EQP_PIN")
	private Integer pin;
	 
	@Column(name="EQP_PUK")
	private Integer puk;
	 
	@Column(name="EQP_DHS")
	@Temporal(TemporalType.DATE)
	private Date dataCadastro;

	@Column(name="EQP_ST")
	private Integer status;
	
	@ManyToOne
	@JoinColumn(name="VER_ID",
			referencedColumnName="VER_ID")
	private Versao versao;
	
	public Equipamento() {}

	public Equipamento(EquipamentoTO equipamentoTO) {
		this.dataCadastro = equipamentoTO.getDataCadastro();
		if (equipamentoTO.getDdd() != null) {
			this.ddd = new Integer(equipamentoTO.getDdd());
		}
		if (equipamentoTO.getPin() != null) {
			this.pin = new Integer(equipamentoTO.getPin());
		}
		if (equipamentoTO.getPuk() != null) {
			this.puk = new Integer(equipamentoTO.getPuk());			
		}
		if (equipamentoTO.getTelefone() != null) {
			this.telefone = new Integer(equipamentoTO.getTelefone());
		}
		if (equipamentoTO.getVersaoTO() != null) {
			this.versao = new Versao(equipamentoTO.getVersaoTO());		
		}
		this.id = equipamentoTO.getId();
		this.serial = equipamentoTO.getSerial();
		if (equipamentoTO.getStatus() != null) {
			this.status = equipamentoTO.getStatus().getStatus();
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getSerial() {
		return serial;
	}

	public void setSerial(String serial) {
		this.serial = serial;
	}

	public Integer getDdd() {
		return ddd;
	}

	public void setDdd(Integer ddd) {
		this.ddd = ddd;
	}

	public Integer getTelefone() {
		return telefone;
	}

	public void setTelefone(Integer telefone) {
		this.telefone = telefone;
	}

	public Integer getPin() {
		return pin;
	}

	public void setPin(Integer pin) {
		this.pin = pin;
	}

	public Integer getPuk() {
		return puk;
	}

	public void setPuk(int puk) {
		this.puk = puk;
	}

	public Date getDataCadastro() {
		return dataCadastro;
	}

	public void setDataCadastro(Date dataCadastro) {
		this.dataCadastro = dataCadastro;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Versao getVersao() {
		return versao;
	}

	public void setVersao(Versao versao) {
		this.versao = versao;
	}

	public EquipamentoTO parseEquipamentoTO() {
		EquipamentoTO equipamentoTO = new EquipamentoTO();
		equipamentoTO.setDataCadastro(this.dataCadastro);
		equipamentoTO.setDdd(this.ddd.toString());
		equipamentoTO.setId(this.id);
		equipamentoTO.setPin(this.pin.toString());
		equipamentoTO.setPuk(this.puk.toString());
		equipamentoTO.setSerial(this.serial);
		if (getStatus() == StatusEnum.ATIVO.getStatus()) {
			equipamentoTO.setStatus(StatusEnum.ATIVO);
		} else {
			equipamentoTO.setStatus(StatusEnum.INATIVO);
		}		
		equipamentoTO.setTelefone(this.telefone.toString());
		equipamentoTO.setVersaoTO(this.versao.parseVersaoTO());
		
		return equipamentoTO;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((dataCadastro == null) ? 0 : dataCadastro.hashCode());
		result = prime * result + ((ddd == null) ? 0 : ddd.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((pin == null) ? 0 : pin.hashCode());
		result = prime * result + ((puk == null) ? 0 : puk.hashCode());
		result = prime * result + ((serial == null) ? 0 : serial.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		result = prime * result
				+ ((telefone == null) ? 0 : telefone.hashCode());
		result = prime * result + ((versao == null) ? 0 : versao.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Equipamento other = (Equipamento) obj;
		if (dataCadastro == null) {
			if (other.dataCadastro != null)
				return false;
		} else if (!dataCadastro.equals(other.dataCadastro))
			return false;
		if (ddd == null) {
			if (other.ddd != null)
				return false;
		} else if (!ddd.equals(other.ddd))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (pin == null) {
			if (other.pin != null)
				return false;
		} else if (!pin.equals(other.pin))
			return false;
		if (puk == null) {
			if (other.puk != null)
				return false;
		} else if (!puk.equals(other.puk))
			return false;
		if (serial == null) {
			if (other.serial != null)
				return false;
		} else if (!serial.equals(other.serial))
			return false;
		if (status == null) {
			if (other.status != null)
				return false;
		} else if (!status.equals(other.status))
			return false;
		if (telefone == null) {
			if (other.telefone != null)
				return false;
		} else if (!telefone.equals(other.telefone))
			return false;
		if (versao == null) {
			if (other.versao != null)
				return false;
		} else if (!versao.equals(other.versao))
			return false;
		return true;
	}
	
	
	
}
